<?php
/**
 * 
 * +------------------------------------------------------------+
 * @category FCache 
 * +------------------------------------------------------------+
 * 文件缓存类，使用文件管理数据缓存
 * +------------------------------------------------------------+
 *
 * @author anzai <236049524@qq.com>
 * @copyright http://www.judianyun.com 2012
 * @version 1.0
 *
 * Modified at : 2012-02-16 11:27:35
 *
 */
class FCache extends Cache {
	///缓存文件存放目录
	private $_cacheDir = null;
	
	///相关配置
	private $_config = array(
		'timeout' => 60,     //缓存文件生存期，不设置或0表示永久
		'prefix' => 'ey_',  //key前缀
		'suffix' => 'cf'     //缓存文件后缀
	);
	
	//构造函数
	function __construct($config=null){
		$this->_cacheDir = isset($config['dir']) ? $config['dir'] : TEMP_PATH . '~filecache' . DIRECTORY_SEPARATOR;
		$this->_config['prefix'] = isset($config['prefix']) ? $config['prefix'] : $this->_config['prefix'];
		$this->_config['suffix'] = isset($config['suffix']) ? $config['suffix'] : $this->_config['suffix'];
		$this->_config['timeout'] = isset($config['timeout']) ? $config['timeout'] : $this->_config['timeout'];
		
		$this->_cacheDir .= date('Ymd') . DIRECTORY_SEPARATOR;
		
		//文件缓存目录不存在自动创建
		is_dir($this->_cacheDir) or Helper::mkdir($this->_cacheDir);
	}
	
	//清除缓存
	function clear(){
		$dir = $this->_cacheDir;
		if (is_dir($dir) && is_readable($dir)) {
			$files = Helper::recurdir($dir,$this->_config['suffix']);
			if (!empty($files)){
				foreach ($files as $vo){
					unlink($vo['dir'].'/'.$vo['name']);
				}
			}
		}
		return $this;
	}
	
	//删除指定的缓存文件
	function delete($key){
		try{
			$file = $this->_getFileName($key);
			if (file_exists($file) && false===unlink($file)){
				throw new E(Helper::L('CLS_FCACHE_DELETE'));
			}
		}catch(E $e){
			$e->getMsg();
		}
		return $this;
	}
	
	///获取缓存数据
	private function _get($key){
		$key = trim($key);
		if(empty($key)) return;
		try {
			$file = $this->_getFileName($key);
			if (file_exists($file)){
				$value = file_get_contents($file);
				if (empty($value)) return;
				if (false!==strpos($value,'<<%-==-%>>')){
					$_tmp = explode('<<%-==-%>>',$value);
					
					///判断缓存生存期是否已过期，过期则删除缓存文件
					if (time()>=$_tmp[0] && !unlink($file)){
						throw new E(Helper::L('CLS_FCACHE_DELETE').$file);
						return ;
					}else{
						$value = unserialize($_tmp[1]);
					}
					unset($_tmp);
				}else
					$value = unserialize($value);
			}else
				return;
		} catch (E $e) {
			$e->getMsg();
		}
		return $value;
	}
	
	//从缓存文件中读取数据，支持获取多个
	function get(){
		$args = func_get_args();
		if (!$args) return;
		
		if(count($args)==1 && trim($args[0]))
			$tmp = $this->_get($args[0]);
		else{
			foreach ($args as $key){
				$key = trim($key);
				if (empty($key)) continue;
				$tmp[$key] = $this->_get($key);
			}
		}
		return empty($tmp) ? null : $tmp;
	}
	
	private function _getFileName($key){
		$filename = md5($this->_config['prefix'].$key);
		return $this->_cacheDir.$filename.'.'.$this->_config['suffix'];
	}
	
	//将数值写入文件缓存
	function set($key,$value){
		$key = trim($key);
		if (empty($key)) return;
		$value = serialize($value); //将要写入缓存的值进行系列化
		$timeout = $this->_config['timeout'];
		$timeout>0 && $value = (time()+$timeout).'<<%-==-%>>'.$value;
		
		if(!Helper::write($this->_getFileName($key),$value))
			throw new E(Helper::L('CLS_FCACHE_WRITE'));
		
		return $this;
	}
}

?>